﻿using System.Collections;
using System.Collections.Generic;
using LitJson;
using UnityEngine;

public class StartChatRequest : BaseRequest {
	protected override void Awake()
	{
		_controllerType = ControllerType.CHAT;
		_methodType = MethodType.START_CHAT;
		base.Awake();
	}

	public void SendRequest(int opponentId)
	{
		JsonData requestJsonData =new JsonData();
		requestJsonData["opponentId"] = opponentId;
		NettyRequest nettyRequest = new NettyRequest();
		nettyRequest.controllerType = (int) _controllerType;
		nettyRequest.methodType = (int) _methodType;
		nettyRequest.data =JsonMapper.ToJson(requestJsonData);
		byte[] dataBytes = ConverterTool.SerialRequestObj(nettyRequest);
		UnityFacade.Instance.ClientManager.SendMsgToServer(dataBytes);

	}
	
	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
		
	}
	
	
	
	public override void OnResponse(string data)
	{
		JsonData respJsonData = JsonMapper.ToObject(data);
		if (respJsonData["state"].Equals("success"))
		{
			ChatVO chatVo = new ChatVO();
			chatVo.ChatId = (int)respJsonData["data"]["chatId"];
			chatVo.Nick = respJsonData["data"]["nick"].ToString();
			chatVo.Type = respJsonData["data"]["type"].ToString();
			chatVo.Avatar = respJsonData["data"]["avatar"].ToString();
			chatVo.UserId = (int)respJsonData["data"]["userId"];
			chatVo.HistoryMsg = respJsonData["data"]["historyMsg"].ToString();
			// 开启ChatPanel,设置chatVo,并刷新展示
			ChatPanel chatPanel =(ChatPanel) UnityFacade.Instance.UiManager.PushPanel(PanelType.Chat);
			chatPanel.ChatVo = chatVo;
			chatPanel.SyncShow();
			
			
		}else if (respJsonData["state"].Equals("fail"))
		{
			MsgPanel msgPanel =(MsgPanel) UnityFacade.Instance.UiManager.PushPanel(PanelType.Msg);
			msgPanel.SyncShow("fail",respJsonData["msg"].ToString());
			Debug.LogError(respJsonData["msg"]);
		}
		
		
		




	}

	
}
